package cn.wolfcode.wolf2w.destination.service.impl;

import cn.wolfcode.wolf2w.destination.entity.TaDestination;
import cn.wolfcode.wolf2w.destination.entity.TaRegion;
import cn.wolfcode.wolf2w.destination.mapper.TaDestinationMapper;
import cn.wolfcode.wolf2w.destination.mapper.TaRegionMapper;
import cn.wolfcode.wolf2w.destination.service.ITaDestinationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 目的地表 服务实现类
 * </p>
 *
 * @author hyz
 * @since 2024-07-23
 */
@Service
public class TaDestinationServiceImpl extends ServiceImpl<TaDestinationMapper, TaDestination> implements ITaDestinationService {

    @Autowired
    private TaDestinationMapper destinationMapper;
    @Autowired
    private TaRegionMapper regionMapper;

    @Override
    public List<TaDestination> queryByRegionId(Long regionId) {
        List<TaDestination> destinationList = null;
        if(regionId == -1){
            //国内
            LambdaQueryWrapper<TaDestination> qw = new LambdaQueryWrapper<>();
            qw.eq(TaDestination::getParentId,1);
//            lambdaQuery().eq(TaDestination::getParentId,1).list();
            destinationList = destinationMapper.selectList(qw);
        }else{
            TaRegion region = regionMapper.selectById(regionId);
            String refIds = region.getRefIds();
            String[] idstrArr = refIds.split(",");
            List<Long> ids = new ArrayList<>();
            for (String s : idstrArr) {
                Long id = Long.valueOf(s);
                ids.add(id);
            }
            LambdaQueryWrapper<TaDestination> qw = new LambdaQueryWrapper<>();
            qw.in(TaDestination::getId,ids);
            destinationList = destinationMapper.selectList(qw);
        }

        for (TaDestination dest : destinationList) {
            Long id = dest.getId();
            List<TaDestination> children = lambdaQuery().eq(TaDestination::getParentId, id).last("limit 10").list();
            dest.setChildren(children);
        }

        return destinationList;
    }

    @Override
    public List<TaDestination> queryToastsByDestId(Long destId) {
        List<TaDestination> list = new ArrayList<>();
        createToasts(destId,list);
//        Collections.reverse(list); //反序排列
        return list;
    }

    @Override
    public List<TaDestination> listIncludeChildren() {
        List<TaDestination> destinationList = null;
        LambdaQueryWrapper<TaDestination> qw = new LambdaQueryWrapper<>();
        destinationList = destinationMapper.selectList(qw);
        for (TaDestination dest : destinationList) {
            Long id = dest.getId();
            List<TaDestination> children = lambdaQuery().eq(TaDestination::getParentId, id).list();
            dest.setChildren(children);
        }
        return destinationList;
    }

    private void createToasts(Long destId,List<TaDestination> list){
        if(destId == null){
            return ;
        }
        TaDestination destination = destinationMapper.selectById(destId);
//        list.add(destination);
//        createToasts(destination.getParentId(),list);
        //回溯
        createToasts(destination.getParentId(),list);
        list.add(destination);
    }

}
